package threadcoreknowledge.sync;

/**
 * 对象锁示例，方法锁形式
 */
public class SynchronizedObjectCodeMethod3 implements Runnable {
    private static SynchronizedObjectCodeMethod3 instance = new SynchronizedObjectCodeMethod3();
    final Object lock1 = new Object();
    final Object lock2 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();


        while (t1.isAlive() || t2.isAlive()) {

        }
        System.out.println("finished");
    }

    @Override
    public void run() {
        method();
    }

    public synchronized void method() {
        System.out.println("sync method - " + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sync method " + Thread.currentThread().getName() + " 运行结束");
    }
    public void extra() {
        System.out.println(33333);
    }
}
